import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;
/**
* @author Kamil Olejnik
*
*/

public class HumanListener extends Client
{
        private PlayerFrame frame;
        private String[] PlayersList;
        /**
        * @param port server port
        * @param name PlayerName
        * @throws ClientException 
        */
        public HumanListener(int port, String IP, String name) throws ClientException
        {
            super(port, IP, name);
        	this.frame = new PlayerFrame(name,this);
        	this.frame.setVisible(true);
        }
        /**
        * @var Error while works = false
        * @param error error
        */
    	public void Error(String error)
    	{
    		JOptionPane.showMessageDialog(null, error,
                    "Error", JOptionPane.ERROR_MESSAGE);
    		this.works = false;
    		
    	}
    	public void out()
    	{
    		this.works = false;
    		JOptionPane.showMessageDialog(null, "you can not continue the game ",
                   "Out of game" , JOptionPane.ERROR_MESSAGE);
    		
    		System.exit(0);
    		
    	}
    	public String Player(int n)
    	{
    		return PlayersList[n];
    	}
    	public void Chat(String msg)
    	{
    		frame.displayMessage(msg);
    	}

    	public void bigBlind(String value)
    	{
    		frame.setBigBlind(stringToInt(value.substring(0,1)),value.substring(1));
    	}
    	
    	public void smallBlind(String value)
    	{
    		System.out.println(value);
    		frame.setSmallBlind(stringToInt(value.substring(0,1)),value.substring(1));
    	}
    	public void sCoins(String value)
    	{
    		 frame.setCoins(stringToInt(value.substring(0,1)),value.substring(1));
    	}
    	public void bet(String value)
    	{
    		 frame.setBet(stringToInt(value.substring(0,1)),value.substring(1));
    	}
    	public void enable(String buttons)
    	{
    		   frame.enableButtons(buttons);
    	}
    	public void dAllin(String Id)
    	{
    		   frame.displayAllin(stringToInt(Id.substring(0,1)));
    	}
    	public void stake(String stake)
    	{
    		frame.displayStake(stake);
    	}
    	public void round(String round)
    	{
    		frame.displayRound(round);
    	}
    	public void cards(String cards)
    	{
    		 for(int i = 0; i < 4; i++) {
                 temporaryHand[i] = Integer.parseInt(cards.substring(2*i,2+2*i));
             }
             frame.displayCards(temporaryHand);
    	}
    	public void Winner(String Winner)
    	{
            //if(Winner.length()%24 != 0)
            	//System.out.println("BLAD!!!!!!!!!!!!!");
            int numberOfPlayers = Winner.length()/9;
            String[] playersHand = new String[numberOfPlayers];
            for(int i = 0; i < numberOfPlayers; i++) {
            	playersHand[i] = Winner.substring(9*i, 9*(i+1) );
            }
             frame.displayPlayerHands(playersHand,Winner.substring(9*numberOfPlayers));
    	}


    	public void Wait(String p)
    	{
    		frame.Wait();
    	}
    	public void ProposalToReject(String player)
    	{	if(!rejecting){
    		int v = 0;
    	    int i = 0;
    		try{
				
				i = Integer.parseInt(player);
				}
				catch(NumberFormatException e)
			{
				System.out.println("Error Invalid Player Number");
			}
    		
    		if (i == this.Id)
    		{
    			frame.YouAreRejecting();
    		}
    		else
    		{

    			
    			v = frame.RejectVote(PlayersList[i]);
    		

    		if (v==0)
    		{
    			this.send("reject_1");
    		}
    		else
    		{
    			this.send("reject_0");
    		}
    		System.out.println(v);}
    	}
    	}
    	
    	public void PlayerRejecting(String player)
    	{
    		int i =-1;
    			try{
				
				i = Integer.parseInt(player);
				}
				catch(NumberFormatException e)
			{
				System.out.println("Error Invalid Player Number");
			}
    			if(i == Id)
    			{
    				frame.YouAreRejected();
    				System.exit(0);
    			}
    			else
    			{

    				frame.PlayerIsRejected(PlayersList[i]);
    			}
    			rejecting = false;
    	}
    	public void PlayerNotRejecting(String player)
    	{
    		int i =-1;
			try{
			
			i = Integer.parseInt(player);
			}
			catch(NumberFormatException e)
		{
			System.out.println("Error Invalid Player Number");
		}
			if(i == Id)
			{
				frame.YouAreNotRejected();
			}
			else
			{

				frame.PlayerIsNotRejected(PlayersList[i]);
			}
			rejecting = false;
    	}
    	public void GameStart()
    	{
    		frame.StartGame();
    	}
    	public void Stop()
    	{
    		frame.stop();
    	}
    	public void StartCoins(String coins)
    	{
    		frame.setCoins(this.Id,coins);
    	}
    	public void StartCards(String cards)
    	{
    		 for(int i = 0; i < 4; i++) {
                 temporaryHand[i] = Integer.parseInt(cards.substring(2*i,2+2*i));
             }
             frame.displayCards(temporaryHand);
    	}
    	public void PlayerDisconected(String player)
    	{
    		System.out.println("PlayerDisconected");
    	}
    	public void dFold(String player)
    	{
    		 frame.displayFold(stringToInt(player));
    	}

    	 public void ClientList(String list){
    	        	System.out.println(list);
    	        	PlayersList = list.split(" ");
    	        	frame.displayPlayersList(PlayersList);

    	       }
                               /* if(line.startsWith("addNewPlayer")){
                                        frame.setPlayer(line.substring(12),false);
                                }
                                else if(line.startsWith("Accepted_")){
                                        frame = new PlayerFrame(name,this);
                                        out.println("Name_"+name);
                                        frame.setVisible(true);
                                }
                                else if (line.startsWith("BadName_")){
                                        JOptionPane.showMessageDialog(null, "Name \"" + shortName +"\" is already used" ,
                                                        "Error", JOptionPane.ERROR_MESSAGE);
                                        System.exit(1);
                                }
                                else if (line.startsWith("previousPlayers_")){
                                        line = line.substring(16);
                                        String temp;
                                        while(!line.equals("")){
                                                temp = line.substring(0,15);
                                                line = line.substring(15);
                                                frame.setPlayer(temp,true);
                                        }
                                }

                                else if (line.startsWith("Delete_")){
                                        frame.deletePlayer(line.substring(7));
                                }*/
                                /*else if (line.startsWith("Amount_")){
                                        frame.displayAmountOfExchangedCards(line.substring(7,22),line.substring(22));
                                }*/ //nie widze żeby to było wysyłane
      
        public String getNick(){
                return nick;
        }
        public void WaitForPlayers()
        {
        	frame.WaitForPlayers();
        }
        //public void sendMessage(String message){
          //      out.println("Send_" + shortName + ":" + message);
        //}

        /**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		int port = 0;
    		String adres = "";
    		String nick = "";
    		try{
    		     port = Integer.parseInt(args[0]);
    		}
    		catch(NumberFormatException e)
    		{
    			System.err.println("Invalid port");
    			System.exit(1);
    		}
    		if(args[1]!=null)
    			adres = args[1];
    		else{
    			System.err.println("Invalid adres");
    			System.exit(1);
    		}
    		if(args[2]!=null)
    			nick = args[2];
    		else{
    			nick = "Player";
    		}
    		HumanListener client = null;
    		try{
    			client = new HumanListener(port ,adres ,nick);
    		}
    		catch(ClientException e){
                System.out.println("Socket problem");
                JOptionPane.showMessageDialog(null, "You cannot join the game\non this port" ,
                                "Error", JOptionPane.ERROR_MESSAGE);
                System.exit(1);
    		}
    		if(client != null)
    		client.start();
 

    	}

     
        
}